package com.car.spdier.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.ParserUtils;
import org.htmlparser.util.SimpleNodeIterator;

import com.car.spdier.HashTable;
import com.car.spdier.service.dao.entity.Punishment;
import com.comm.cache.entity.Punishments;
public class ShangHaiParseImpl extends BaseParseService {

	public List<Punishment> parse(Map map) {
		// TODO Auto-generated method stub
		return null;
	}

	public Punishments parse(String htmlSourceCode) {
		Punishments listRS =new Punishments();
		//Parser httpParser = new Parser("c://1map.html");
		//httpParser.setEncoding("GB2312");
		Parser httpParser = Parser.createParser(htmlSourceCode, HashTable
				.getCharset(HashTable.shanghaiCode).toString());
		NodeFilter filter_tables = new AndFilter((new TagNameFilter("table")),
				new HasAttributeFilter("bgcolor", "#6B654D"));
		NodeList nodeTables = null;
		try {
			nodeTables = httpParser.parse(filter_tables);
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Node tableOne = nodeTables.elementAt(0);
		if(tableOne==null){
			return null;
		}
		String constellation = tableOne.getFirstChild().getNextSibling()
				.toHtml();

		Node tableTwo = nodeTables.elementAt(1);
		NodeList tableTwo_nodelist = tableTwo.getChildren();
		Node tableThree = nodeTables.elementAt(2);
		if (tableThree instanceof TableTag) {
			TableTag tableTag = (TableTag) tableThree;
			listRS = parserTable(tableTag);
			
		}
       return listRS;
	}
	/*protected Punishments parserTable(TableTag tableTag) {
		Punishments punishments = new Punishments();

		SimpleNodeIterator nodes = tableTag.elements();
		Punishment punishment = new Punishment();
		while (nodes.hasMoreNodes()) {
			Node node = nodes.nextNode();
			if (node instanceof TableRow) {
				TableRow row = (TableRow) node;
				System.out.println("--TableRow--处理开始---");

				SimpleNodeIterator colNodes = row.elements();
				while (colNodes.hasMoreNodes()) {
					Node colNode = colNodes.nextNode();
					if (colNode instanceof TableColumn) {
						TableColumn col = (TableColumn) colNode;
						String colText = ParserUtils.trimAllTags(
								col.getChildrenHTML(), false);
						colText = ParserUtils.removeEscapeCharacters(colText);
						colText = ParserUtils.removeTrailingBlanks(colText);
						colText = ParserUtils.removeChars(colText, ' ');
						if (punishment.isFull()) {
							punishments.add(punishment.toPunishmentObj());
							punishment = new Punishment();
						}
						punishment.setValue(colText);// 设置当前行值
						preprocePunishment(punishment, colText);// 设置下一行的标志

					}
				}
				System.out.println("--TableRow--处理结束---");
			}

		}
		return punishments;

	}

	*//**
	 * 上海违章查询字段处理
	 * 
	 * @param prs
	 * @param colString
	 *//*
	protected void preprocePunishment(Punishment prs, String colString) {
		if (colString.startsWith("违法时间：")) {
			System.out.println(colString);
			prs.isRSDate = true;
			return;
		}
		if (colString.startsWith("编号：")) {
			System.out.println(colString);
			prs.isRS_code = true;
			return;
		}
		if (colString.startsWith("执法机关代码")) {
			System.out.println(colString);
			prs.isOrg_code = true;
			return;
		}
		if (colString.startsWith("违法地点：")) {
			System.out.println(colString);
			prs.isSpace = true;
			return;
		}
		if (colString.startsWith("采集机关：")) {
			System.out.println(colString);
			prs.isOrg_name = true;
			return;
		}
		if (colString.startsWith("违法内容：")) {
			System.out.println(colString);
			prs.isContent = true;
			return;
		}
		if (colString.startsWith("违反条款：")) {
			System.out.println(colString);
			prs.isClause = true;
			return;
		}
		if (colString.startsWith("处罚依据：")) {
			System.out.println(colString);
			prs.isAccord = true;
			return;
		}
		if (colString.startsWith("处罚内容：")) {
			System.out.println(colString);
			prs.isPunishment = true;
			return;
		}
		if (colString.startsWith("其他措施：")) {
			System.out.println(colString);
			prs.isPunishment = true;
			return;
		}

	}*/
	public Punishments parse(File file) {
		// TODO Auto-generated method stub
		return null;
	}

}
